# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#

# Uncomment the line if you want fastlane to automatically update itself
#update_fastlane
opt_out_usage

default_platform(:ios)

platform :ios do
  desc "Generate new localized screenshots"
  lane :screenshots do
    capture_screenshots(project: "Sources/iOSSampleApp.xcodeproj",
                        xcargs: "-skipPackagePluginValidation",
                        scheme: "iOSSampleApp")
  end
end

desc "Run all unit tests"
lane :tests do
  run_tests(devices: ["iPhone 14"],
            project: "Sources/iOSSampleApp.xcodeproj",
            skip_testing: "iOSSampleAppUITests",
            xcargs: "-skipPackagePluginValidation",
            scheme: "iOSSampleApp")
end

desc "Updates libraries licenses"
lane :update_licenses do
  require 'xcodeproj'

  dependencies = []

  project = Xcodeproj::Project.open("../Sources/iOSSampleApp.xcodeproj")

  project.targets.each do |target|
      next if target.name.end_with? "Tests"
    
      target.package_product_dependencies.each do |dependency|            
          dependencies.push((dependency.package.repositoryURL.sub "https://github.com/", "github \"") + "\"")
      end
  end

  dependencies = dependencies.uniq.sort

  File.open("../support/Cartfile.spm", "w+") do |f|
      f.puts(dependencies)
  end

  Dir.chdir ".." do    
    sh "swift support/fetch_licenses.swift support/Cartfile.spm Sources/iOSSampleApp/Data"
  end

  File.delete("../support/Cartfile.spm")
end